package com.fr.dialog.core;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.StringUtils;
import com.fr.cell.core.GUICoreUtils;
import com.fr.cell.core.layout.LayoutFactory;
import com.fr.dialog.BaseDialog;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.ScriptUtils;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.util.Utils;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ParameterInputDialog extends BaseDialog
{
  private Map editorNameMap;

  public static ParameterInputDialog showWindow(Window paramWindow, Parameter[] paramArrayOfParameter)
  {
    if (paramWindow instanceof Frame)
      return new ParameterInputDialog((Frame)paramWindow, paramArrayOfParameter);
    return new ParameterInputDialog((Dialog)paramWindow, paramArrayOfParameter);
  }

  private ParameterInputDialog(Frame paramFrame, Parameter[] paramArrayOfParameter)
  {
    super(paramFrame);
    initComponents(paramArrayOfParameter);
  }

  private ParameterInputDialog(Dialog paramDialog, Parameter[] paramArrayOfParameter)
  {
    super(paramDialog);
    initComponents(paramArrayOfParameter);
  }

  protected void initComponents(Parameter[] paramArrayOfParameter)
  {
    int i;
    super.initComponents();
    JPanel localJPanel1 = (JPanel)getContentPane();
    JPanel localJPanel2 = new JPanel();
    localJPanel1.add(localJPanel2, "Center");
    localJPanel2.setLayout(new BoxLayout(localJPanel2, 1));
    localJPanel2.setBorder(BorderFactory.createTitledBorder(Inter.getLocText("Parameters") + ":"));
    FlowTableLayoutHelper localFlowTableLayoutHelper = new FlowTableLayoutHelper();
    this.editorNameMap = new HashMap();
    ArrayList localArrayList = new ArrayList();
    if ((paramArrayOfParameter != null) && (paramArrayOfParameter.length > 0))
      for (i = 0; i < paramArrayOfParameter.length; ++i)
      {
        Parameter localParameter = paramArrayOfParameter[i];
        if (localArrayList.contains(localParameter.getName()))
          break label405:
        JTextField localJTextField = new JTextField();
        Object localObject = localParameter.getValue();
        if ((localObject instanceof Formula) || ((localObject instanceof String) && (((String)localObject).startsWith("="))))
        {
          localJTextField.setText(Inter.getLocText("Utils-Default_Value") + "(" + Inter.getLocText("ParameterD-DClick_Edit") + ")");
          localJTextField.setEditable(false);
          localJTextField.addMouseListener(new MouseListener(this, localObject, localJTextField)
          {
            private final Object val$pv;
            private final JTextField val$textF;
            private final ParameterInputDialog this$0;

            public void mouseClicked()
            {
              if (paramMouseEvent.getClickCount() >= 2)
              {
                Object localObject = null;
                try
                {
                  if (this.val$pv instanceof Formula)
                    localObject = ScriptUtils.executeFormula(Calculator.createCalculator(), (Formula)this.val$pv);
                  else
                    localObject = ScriptUtils.executeFormula(Calculator.createCalculator(), new Formula((String)this.val$pv));
                }
                catch (RuntimeException localRuntimeException)
                {
                  FRContext.getLogger().log(Level.WARNING, localRuntimeException.getMessage(), localRuntimeException);
                }
                this.val$textF.setText(Utils.objectToString(localObject));
                this.val$textF.setEditable(true);
              }
            }

            public void mouseEntered()
            {
            }

            public void mouseExited()
            {
            }

            public void mousePressed()
            {
            }

            public void mouseReleased()
            {
            }
          });
        }
        else if (localObject != null)
        {
          localJTextField.setText(Utils.objectToString(localObject));
        }
        JPanel localJPanel3 = new JPanel();
        localJPanel3.setLayout(LayoutFactory.createBorderLayout());
        localJPanel3.add(localJTextField, "Center");
        localJPanel3.setPreferredSize(new Dimension(180, localJPanel3.getPreferredSize().height));
        String str = localParameter.getName();
        if (StringUtils.isNotBlank(localParameter.getName()))
          str = localParameter.getName();
        localJPanel2.add(localFlowTableLayoutHelper.createLabelFlowPane(str + ":", localJPanel3));
        this.editorNameMap.put(localJTextField, localParameter.getName());
        localArrayList.add(localParameter.getName());
      }
    label405: localFlowTableLayoutHelper.adjustLabelWidth();
    localJPanel1.add(createControlButtonPane(), "South");
    setModal(true);
    setTitle(Inter.getLocText("Parameters"));
    pack();
    if (getOwner() != null)
      GUICoreUtils.setWindowCenter(getOwner(), this);
    setVisible(true);
  }

  public Map update()
  {
    HashMap localHashMap = new HashMap();
    Iterator localIterator = this.editorNameMap.entrySet().iterator();
    while (true)
    {
      String str1;
      String str2;
      while (true)
      {
        Map.Entry localEntry;
        JTextField localJTextField;
        while (true)
        {
          if (!(localIterator.hasNext()))
            break label122;
          localEntry = (Map.Entry)localIterator.next();
          localJTextField = (JTextField)localEntry.getKey();
          if (localJTextField.isEditable())
            break;
        }
        str1 = (String)localEntry.getValue();
        str2 = localJTextField.getText();
        Number localNumber = Utils.string2Number(str2);
        if (localNumber == null)
          break;
        localHashMap.put(str1, localNumber);
      }
      localHashMap.put(str1, str2);
    }
    label122: return localHashMap;
  }

  private static class FlowTableLayoutHelper
  {
    private List labelList = new ArrayList();

    public JPanel createLabelFlowPane(String paramString, JComponent paramJComponent)
    {
      JPanel localJPanel = new JPanel();
      localJPanel.setLayout(new FlowLayout(0, 4, 2));
      JLabel localJLabel = new JLabel(paramString);
      localJPanel.add(localJLabel);
      localJLabel.setHorizontalAlignment(2);
      this.labelList.add(localJLabel);
      localJPanel.add(paramJComponent);
      return localJPanel;
    }

    public void adjustLabelWidth()
    {
      int i = 0;
      for (int j = 0; j < this.labelList.size(); ++j)
        i = Math.max(i, ((JLabel)this.labelList.get(j)).getPreferredSize().width);
      for (j = 0; j < this.labelList.size(); ++j)
      {
        JLabel localJLabel = (JLabel)this.labelList.get(j);
        Dimension localDimension = new Dimension(i, localJLabel.getPreferredSize().height);
        localJLabel.setPreferredSize(localDimension);
        localJLabel.setSize(localDimension);
        localJLabel.setMinimumSize(localDimension);
      }
    }
  }
}